package com.mmall.concurrency.example.sync;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mmall.concurrency.example.atomic.AtomicExample3;

// 修饰一个类，一个静态方法，它的作用域是整个类
@Slf4j
public class SynchronizedExample2 {
	private static Logger log = LoggerFactory.getLogger(AtomicExample3.class);

    // 修饰一个类
    public static void test1(int j){
        synchronized (SynchronizedExample2.class){
            for(int i = 0; i < 10; i++){
                log.info("test1 {} - {}", j, i);
            }
        }
    }
    // 修饰一个静态方法
    public static synchronized void test2(int j){
        for(int i = 0; i < 10; i++){
            log.info("test2 {} - {}", j, i);
        }
    }

    public static void main(String[] args) {
        SynchronizedExample2 synchronizedExample1 = new SynchronizedExample2();
        SynchronizedExample2 synchronizedExample2 = new SynchronizedExample2();
        ExecutorService service = Executors.newCachedThreadPool();
        service.execute(()->{
            synchronizedExample1.test2(1);
        });
        service.execute(()->{
            synchronizedExample2.test2(2);
        });
    }
}
